package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"hcy-api/lib/http"
	"hcy-api/lib/id"
	"hcy-api/structs/enum"
	"idp/global"
	"path/filepath"
	"strings"
)

var FileApi = new(fileApi)

type fileApi struct {
}

func (api *fileApi) UploadPic(c *gin.Context) {
	f, err := c.FormFile("file")
	if err != nil {
		http.BadWithParamsError(c)
		return
	}
	suf := f.Filename[strings.LastIndex(f.Filename, "."):]
	filename := fmt.Sprintf("%d%s", id.GetSnowId(), suf)
	err = c.SaveUploadedFile(f, filepath.Join(global.IdpConf.PicPath, filename))
	if err != nil {
		http.BadWithCode(c, enum.ErrorMap.UploadFileErrForIdp)
		return
	}
	http.Ok(c, filename)
}
